package com.leyou.item.server;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.ExceptionEnum.ExceptionEnum;
import com.leyou.exception.LyException;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.pojo.BrandDTO;
import com.leyou.pojo.CategoryDTO;
import com.leyou.utils.BeanHelper;
import com.leyou.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;
    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows, String key, String sortBy, Boolean desc) {

        PageHelper.startPage(page,rows);

        Example example = new Example(Brand.class);
        if (StringUtils.isNoneBlank(key)){
            example.createCriteria().orLike("name","%"+key+"%")
                    .orEqualTo("letter",key.toUpperCase());
        }
        if (StringUtils.isNoneBlank(sortBy)){
            String orderByClause = sortBy + (desc ? " desc" : " asc");
            example.setOrderByClause(orderByClause);
        }
        List<Brand> brands  = brandMapper.selectByExample(example);


        if (CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        PageInfo<Brand> info = new PageInfo<>(brands);

        List<BrandDTO> list = BeanHelper.copyWithCollection(brands,BrandDTO.class);

        return new PageResult<>(info.getTotal(),list);

    }
    @Transactional
    public void saveBrand(BrandDTO brandDTO, List<Long> ids) {
        Brand brand = BeanHelper.copyProperties(brandDTO,Brand.class);
        brand.setId(null);
        if (brandMapper.insertSelective(brand) != 1)
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        System.out.println(brand.getId());
        if (brandMapper.insertCategoryBrand(brand.getId(),ids) != ids.size())
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
    }
    @Transactional
    public void updateBrand(BrandDTO brandDTO, List<Long> ids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        int count = brandMapper.updateByPrimaryKeySelective(brand);
        if(count != 1){
            // 更新失败，抛出异常
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
        // 删除中间表数据
        brandMapper.deleteCategoryBrand(brand.getId());

        // 重新插入中间表数据
        count = brandMapper.insertCategoryBrand(brand.getId(), ids);
        if(count != ids.size()){
            // 新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }
    @Transactional
    public void deleteByBrandId(Long id) {
        Brand brand = new Brand();
        brand.setId(id);
        List<Brand> select = brandMapper.select(brand);
        if (CollectionUtils.isEmpty(select)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        brandMapper.deleteCategoryBrand(brand.getId());
        brandMapper.delete(brand);
    }

    public BrandDTO queryById(Long brandId) {
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        if(brand == null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }

    public List<BrandDTO> queryByCategoryId(Long id) {
        List<Brand> brands = brandMapper.queryByCategoryId(id);
        if (CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(brands,BrandDTO.class);
    }
}
